home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / perl5 / IO / WrapTie.pm < prev   
Encoding:
Perl POD Document  |  2005-02-10  |  14.4 KB  |  492 lines

  1. # SEE DOCUMENTATION AT BOTTOM OF FILE
  2.  
  3.  
  4. #------------------------------------------------------------
  5. package IO::WrapTie;
  6. #------------------------------------------------------------
  7. require 5.004;              ### for tie
  8. use strict;
  9. use vars qw(@ISA @EXPORT $VERSION);
  10. use Exporter;
  11.  
  12. # Inheritance, exporting, and package version:
  13. @ISA     = qw(Exporter);
  14. @EXPORT  = qw(wraptie);
  15. $VERSION = "2.110";
  16.  
  17. # Function, exported.
  18. sub wraptie {
  19.     IO::WrapTie::Master->new(@_);
  20. }
  21.  
  22. # Class method; BACKWARDS-COMPATIBILITY ONLY!
  23. sub new { 
  24.     shift; 
  25.     IO::WrapTie::Master->new(@_);
  26. }
  27.  
  28.  
  29.  
  30. #------------------------------------------------------------
  31. package IO::WrapTie::Master;
  32. #------------------------------------------------------------
  33.  
  34. use strict;
  35. use vars qw(@ISA $AUTOLOAD);
  36. use IO::Handle;
  37.  
  38. # We inherit from IO::Handle to get methods which invoke i/o operators,
  39. # like print(), on our tied handle:
  40. @ISA = qw(IO::Handle);
  41.  
  42. #------------------------------
  43. # new SLAVE, TIEARGS...
  44. #------------------------------
  45. # Create a new subclass of IO::Handle which...
  46. #
  47. #   (1) Handles i/o OPERATORS because it is tied to an instance of 
  48. #       an i/o-like class, like IO::Scalar.
  49. #
  50. #   (2) Handles i/o METHODS by delegating them to that same tied object!.
  51. #
  52. # Arguments are the slave class (e.g., IO::Scalar), followed by all 
  53. # the arguments normally sent into that class's TIEHANDLE method.
  54. # In other words, much like the arguments to tie().  :-)
  55. #
  56. # NOTE:
  57. # The thing $x we return must be a BLESSED REF, for ($x->print()).
  58. # The underlying symbol must be a FILEHANDLE, for (print $x "foo").
  59. # It has to have a way of getting to the "real" back-end object...
  60. #
  61. sub new {
  62.     my $master = shift;
  63.     my $io = IO::Handle->new;   ### create a new handle
  64.     my $slave = shift;
  65.     tie *$io, $slave, @_;       ### tie: will invoke slave's TIEHANDLE
  66.     bless $io, $master;         ### return a master
  67. }
  68.  
  69. #------------------------------
  70. # AUTOLOAD
  71. #------------------------------
  72. # Delegate method invocations on the master to the underlying slave.
  73. #
  74. sub AUTOLOAD {
  75.     my $method = $AUTOLOAD;
  76.     $method =~ s/.*:://;
  77.     my $self = shift; tied(*$self)->$method(\@_);
  78. }
  79.  
  80. #------------------------------
  81. # PRELOAD
  82. #------------------------------
  83. # Utility.
  84. #
  85. # Most methods like print(), getline(), etc. which work on the tied object 
  86. # via Perl's i/o operators (like 'print') are inherited from IO::Handle.
  87. #
  88. # Other methods, like seek() and sref(), we must delegate ourselves.
  89. # AUTOLOAD takes care of these.
  90. #
  91. # However, it may be necessary to preload delegators into your
  92. # own class.  PRELOAD will do this.
  93. #
  94. sub PRELOAD {
  95.     my $class = shift;
  96.     foreach (@_) {
  97.     eval "sub ${class}::$_ { my \$s = shift; tied(*\$s)->$_(\@_) }";
  98.     }    
  99. }
  100.  
  101. # Preload delegators for some standard methods which we can't simply
  102. # inherit from IO::Handle... for example, some IO::Handle methods 
  103. # assume that there is an underlying file descriptor.
  104. #
  105. PRELOAD IO::WrapTie::Master 
  106.     qw(open opened close read clearerr eof seek tell setpos getpos);
  107.  
  108.  
  109.  
  110. #------------------------------------------------------------
  111. package IO::WrapTie::Slave;
  112. #------------------------------------------------------------
  113. # Teeny private class providing a new_tie constructor...
  114. #
  115. # HOW IT ALL WORKS:
  116. # Slaves inherit from this class.
  117. #
  118. # When you send a new_tie() message to a tie-slave class (like IO::Scalar),
  119. # it first determines what class should provide its master, via TIE_MASTER.
  120. # In this case, IO::Scalar->TIE_MASTER would return IO::Scalar::Master.
  121. # Then, we create a new master (an IO::Scalar::Master) with the same args
  122. # sent to new_tie.
  123. #
  124. # In general, the new() method of the master is inherited directly 
  125. # from IO::WrapTie::Master.
  126. #
  127. sub new_tie {
  128.     my $self = shift;
  129.     $self->TIE_MASTER->new($self,@_);     ### e.g., IO::Scalar::Master->new(@_)
  130. }
  131.  
  132. # Default class method for new_tie().
  133. # All your tie-slave class (like IO::Scalar) has to do is override this 
  134. # method with a method that returns the name of an appropriate "master"
  135. # class for tying that slave.
  136. #
  137. sub TIE_MASTER { 'IO::WrapTie::Master' }
  138.  
  139. #------------------------------
  140. 1;
  141. __END__
  142.  
  143.  
  144. package IO::WrapTie;      ### for doc generator
  145.  
  146.  
  147. =head1 NAME
  148.  
  149. IO::WrapTie - wrap tieable objects in IO::Handle interface
  150.  
  151. I<This is currently Alpha code, released for comments.  
  152.   Please give me your feedback!>
  153.  
  154.  
  155. =head1 SYNOPSIS
  156.  
  157. First of all, you'll need tie(), so:
  158.  
  159.    require 5.004;
  160.  
  161. I<Function interface (experimental).>
  162. Use this with any existing class...
  163.  
  164.    use IO::WrapTie;
  165.    use FooHandle;                  ### implements TIEHANDLE interface
  166.  
  167.    ### Suppose we want a "FooHandle->new(&FOO_RDWR, 2)".
  168.    ### We can instead say...
  169.  
  170.    $FH = wraptie('FooHandle', &FOO_RDWR, 2); 
  171.  
  172.    ### Now we can use...    
  173.    print $FH "Hello, ";            ### traditional operator syntax...
  174.    $FH->print("world!\n");         ### ...and OO syntax as well!
  175.  
  176. I<OO interface (preferred).>
  177. You can inherit from the IO::WrapTie::Slave mixin to get a
  178. nifty C<new_tie()> constructor...
  179.  
  180.    #------------------------------    
  181.    package FooHandle;                        ### a class which can TIEHANDLE
  182.  
  183.    use IO::WrapTie;  
  184.    @ISA = qw(IO::WrapTie::Slave);            ### inherit new_tie()
  185.    ...
  186.  
  187.  
  188.    #------------------------------    
  189.    package main; 
  190.  
  191.    $FH = FooHandle->new_tie(&FOO_RDWR, 2);   ### $FH is an IO::WrapTie::Master
  192.    print $FH "Hello, ";                      ### traditional operator syntax
  193.    $FH->print("world!\n");                   ### OO syntax
  194.  
  195. See IO::Scalar as an example.  It also shows you how to create classes
  196. which work both with and without 5.004.
  197.  
  198.  
  199. =head1 DESCRIPTION
  200.  
  201. Suppose you have a class C<FooHandle>, where...
  202.  
  203. =over 4
  204.  
  205. =item *
  206.  
  207. B<FooHandle does not inherit from IO::Handle;> that is, it performs
  208. filehandle-like I/O, but to something other than an underlying
  209. file descriptor.  Good examples are IO::Scalar (for printing to a
  210. string) and IO::Lines (for printing to an array of lines).
  211.  
  212. =item *
  213.  
  214. B<FooHandle implements the TIEHANDLE interface> (see L<perltie>);
  215. that is, it provides methods TIEHANDLE, GETC, PRINT, PRINTF,
  216. READ, and READLINE.
  217.  
  218. =item *
  219.  
  220. B<FooHandle implements the traditional OO interface> of
  221. FileHandle and IO::Handle; i.e., it contains methods like getline(), 
  222. read(), print(), seek(), tell(), eof(), etc.
  223.  
  224. =back
  225.  
  226.  
  227. Normally, users of your class would have two options:
  228.  
  229.  
  230. =over 4
  231.  
  232. =item *
  233.  
  234. B<Use only OO syntax,> and forsake named I/O operators like 'print'.
  235.  
  236. =item * 
  237.  
  238. B<Use with tie,> and forsake treating it as a first-class object 
  239. (i.e., class-specific methods can only be invoked through the underlying
  240. object via tied()... giving the object a "split personality").
  241.  
  242. =back
  243.  
  244.  
  245. But now with IO::WrapTie, you can say:
  246.  
  247.     $WT = wraptie('FooHandle', &FOO_RDWR, 2);
  248.     $WT->print("Hello, world\n");   ### OO syntax
  249.     print $WT "Yes!\n";             ### Named operator syntax too!
  250.     $WT->weird_stuff;               ### Other methods!
  251.  
  252. And if you're authoring a class like FooHandle, just have it inherit 
  253. from C<IO::WrapTie::Slave> and that first line becomes even prettier:
  254.  
  255.     $WT = FooHandle->new_tie(&FOO_RDWR, 2);
  256.  
  257. B<The bottom line:> now, almost any class can look and work exactly like
  258. an IO::Handle... and be used both with OO and non-OO filehandle syntax.
  259.  
  260.  
  261. =head1 HOW IT ALL WORKS
  262.  
  263.  
  264. =head2 The data structures
  265.  
  266. Consider this example code, using classes in this distribution:
  267.  
  268.     use IO::Scalar;
  269.     use IO::WrapTie;
  270.  
  271.     $WT = wraptie('IO::Scalar',\$s);
  272.     print $WT "Hello, ";
  273.     $WT->print("world!\n");
  274.  
  275. In it, the wraptie() function creates a data structure as follows:
  276.  
  277.                           * $WT is a blessed reference to a tied filehandle
  278.               $WT           glob; that glob is tied to the "Slave" object.
  279.                |          * You would do all your i/o with $WT directly.
  280.                |       
  281.                |
  282.                |     ,---isa--> IO::WrapTie::Master >--isa--> IO::Handle
  283.                V    /
  284.         .-------------. 
  285.         |             | 
  286.         |             |   * Perl i/o operators work on the tied object,  
  287.         |  "Master"   |     invoking the TIEHANDLE methods.
  288.         |             |   * Method invocations are delegated to the tied 
  289.         |             |     slave.
  290.         `-------------' 
  291.                |    
  292.     tied(*$WT) |     .---isa--> IO::WrapTie::Slave
  293.                V    /   
  294.         .-------------.
  295.         |             |
  296.         |   "Slave"   |   * Instance of FileHandle-like class which doesn't
  297.         |             |     actually use file descriptors, like IO::Scalar.
  298.         |  IO::Scalar |   * The slave can be any kind of object.
  299.         |             |   * Must implement the TIEHANDLE interface.
  300.         `-------------'
  301.  
  302.  
  303. I<NOTE:> just as an IO::Handle is really just a blessed reference to a 
  304. I<traditional> filehandle glob... so also, an IO::WrapTie::Master 
  305. is really just a blessed reference to a filehandle 
  306. glob I<which has been tied to some "slave" class.>
  307.  
  308.  
  309. =head2 How wraptie() works
  310.  
  311. =over 4
  312.  
  313. =item 1.
  314.  
  315. The call to function C<wraptie(SLAVECLASS, TIEARGS...)> is 
  316. passed onto C<IO::WrapTie::Master::new()>.  
  317. Note that class IO::WrapTie::Master is a subclass of IO::Handle.
  318.  
  319. =item 2.
  320.  
  321. The C<IO::WrapTie::Master::new> method creates a new IO::Handle object,
  322. reblessed into class IO::WrapTie::Master.  This object is the I<master>, 
  323. which will be returned from the constructor.  At the same time...
  324.  
  325. =item 3.
  326.  
  327. The C<new> method also creates the I<slave>: this is an instance 
  328. of SLAVECLASS which is created by tying the master's IO::Handle 
  329. to SLAVECLASS via C<tie(HANDLE, SLAVECLASS, TIEARGS...)>.  
  330. This call to C<tie()> creates the slave in the following manner:
  331.  
  332. =item 4.
  333.  
  334. Class SLAVECLASS is sent the message C<TIEHANDLE(TIEARGS...)>; it 
  335. will usually delegate this to C<SLAVECLASS::new(TIEARGS...)>, resulting
  336. in a new instance of SLAVECLASS being created and returned.
  337.  
  338. =item 5.
  339.  
  340. Once both master and slave have been created, the master is returned
  341. to the caller.  
  342.  
  343. =back
  344.  
  345.  
  346. =head2 How I/O operators work (on the master)
  347.  
  348. Consider using an i/o operator on the master:
  349.  
  350.     print $WT "Hello, world!\n";   
  351.  
  352. Since the master ($WT) is really a [blessed] reference to a glob, 
  353. the normal Perl i/o operators like C<print> may be used on it.
  354. They will just operate on the symbol part of the glob.
  355.  
  356. Since the glob is tied to the slave, the slave's PRINT method 
  357. (part of the TIEHANDLE interface) will be automatically invoked.  
  358.  
  359. If the slave is an IO::Scalar, that means IO::Scalar::PRINT will be 
  360. invoked, and that method happens to delegate to the C<print()> method 
  361. of the same class.  So the I<real> work is ultimately done by 
  362. IO::Scalar::print().
  363.  
  364.  
  365. =head2 How methods work (on the master)
  366.  
  367. Consider using a method on the master:
  368.  
  369.     $WT->print("Hello, world!\n");
  370.  
  371. Since the master ($WT) is blessed into the class IO::WrapTie::Master,
  372. Perl first attempts to find a C<print()> method there.  Failing that,
  373. Perl next attempts to find a C<print()> method in the superclass,
  374. IO::Handle.  It just so happens that there I<is> such a method;
  375. that method merely invokes the C<print> i/o operator on the self object...
  376. and for that, see above!
  377.  
  378. But let's suppose we're dealing with a method which I<isn't> part
  379. of IO::Handle... for example:
  380.  
  381.     my $sref = $WT->sref;
  382.  
  383. In this case, the intuitive behavior is to have the master delegate the
  384. method invocation to the slave (now do you see where the designations
  385. come from?).  This is indeed what happens: IO::WrapTie::Master contains
  386. an AUTOLOAD method which performs the delegation.  
  387.  
  388. So: when C<sref()> can't be found in IO::Handle, the AUTOLOAD method
  389. of IO::WrapTie::Master is invoked, and the standard behavior of
  390. delegating the method to the underlying slave (here, an IO::Scalar)
  391. is done.
  392.  
  393. Sometimes, to get this to work properly, you may need to create 
  394. a subclass of IO::WrapTie::Master which is an effective master for
  395. I<your> class, and do the delegation there.
  396.  
  397.  
  398.  
  399.  
  400. =head1 NOTES
  401.  
  402. B<Why not simply use the object's OO interface?> 
  403.     Because that means forsaking the use of named operators
  404. like print(), and you may need to pass the object to a subroutine
  405. which will attempt to use those operators:
  406.  
  407.     $O = FooHandle->new(&FOO_RDWR, 2);
  408.     $O->print("Hello, world\n");  ### OO syntax is okay, BUT....
  409.  
  410.     sub nope { print $_[0] "Nope!\n" }
  411.  X  nope($O);                     ### ERROR!!! (not a glob ref)
  412.  
  413.  
  414. B<Why not simply use tie()?> 
  415.     Because (1) you have to use tied() to invoke methods in the
  416. object's public interface (yuck), and (2) you may need to pass 
  417. the tied symbol to another subroutine which will attempt to treat 
  418. it in an OO-way... and that will break it:
  419.  
  420.     tie *T, 'FooHandle', &FOO_RDWR, 2; 
  421.     print T "Hello, world\n";   ### Operator is okay, BUT... 
  422.  
  423.     tied(*T)->other_stuff;      ### yuck! AND...
  424.  
  425.     sub nope { shift->print("Nope!\n") }
  426.  X  nope(\*T);                  ### ERROR!!! (method "print" on unblessed ref)
  427.  
  428.  
  429. B<Why a master and slave? 
  430.   Why not simply write FooHandle to inherit from IO::Handle?>
  431.     I tried this, with an implementation similar to that of IO::Socket.  
  432. The problem is that I<the whole point is to use this with objects
  433. that don't have an underlying file/socket descriptor.>.
  434. Subclassing IO::Handle will work fine for the OO stuff, and fine with 
  435. named operators I<if> you tie()... but if you just attempt to say:
  436.  
  437.     $IO = FooHandle->new(&FOO_RDWR, 2);
  438.     print $IO "Hello!\n";
  439.  
  440. you get a warning from Perl like:
  441.  
  442.     Filehandle GEN001 never opened
  443.  
  444. because it's trying to do system-level i/o on an (unopened) file 
  445. descriptor.  To avoid this, you apparently have to tie() the handle...
  446. which brings us right back to where we started!  At least the
  447. IO::WrapTie mixin lets us say:
  448.  
  449.     $IO = FooHandle->new_tie(&FOO_RDWR, 2);
  450.     print $IO "Hello!\n";
  451.  
  452. and so is not I<too> bad.  C<:-)>
  453.  
  454.  
  455. =head1 WARNINGS
  456.  
  457. Remember: this stuff is for doing FileHandle-like i/o on things
  458. I<without underlying file descriptors>.  If you have an underlying
  459. file descriptor, you're better off just inheriting from IO::Handle.
  460.  
  461. B<Be aware that new_tie() always returns an instance of a
  462. kind of IO::WrapTie::Master...> it does B<not> return an instance 
  463. of the i/o class you're tying to!  
  464.  
  465. Invoking some methods on the master object causes AUTOLOAD to delegate
  466. them to the slave object... so it I<looks> like you're manipulating a 
  467. "FooHandle" object directly, but you're not.
  468.  
  469. I have not explored all the ramifications of this use of tie().
  470. I<Here there be dragons>.
  471.  
  472.  
  473. =head1 VERSION
  474.  
  475. $Id: WrapTie.pm,v 1.2 2005/02/10 21:21:53 dfs Exp $
  476.  
  477.  
  478. =head1 AUTHOR
  479.  
  480. =item Primary Maintainer
  481.  
  482. David F. Skoll (F<dfs@roaringpenguin.com>).
  483.  
  484. =item Original Author
  485.  
  486. Eryq (F<eryq@zeegee.com>).
  487. President, ZeeGee Software Inc (F<http://www.zeegee.com>).
  488.  
  489. =cut
  490.  
  491.